﻿#region
/* ===============================================
 * 作者：guopeng           时间：2012/2/4 10:15:25
 * 
 * 
 * 类描述：基础用户实体。只包含账户名和密码
 * 
 * 
 * =============================================== */
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations;
using ueEngine.Biz.Inf.Entities.DBSupport;
using ueEngine.Framework.Exception;
using ueEngine.Framework.Caching;

namespace ueEngine.Biz.Inf.Entities
{
    [Table("Member")]
    public class Member : BaseEntity<Member>
    {
        public Member() { }
        public override Member Get(System.Linq.Expressions.Expression<Func<Member, bool>> where)
        {
            using (Context ctx = new Context())
            {
                var item = ctx.Member.FirstOrDefault(where);
                if (item != null)
                {
                    this.Id = item.Id;
                    this.Image = item.Image;
                    this.IsActivate = item.IsActivate;
                    this.Location = item.Location;
                    this.NickName = item.NickName;
                    this.Password = item.Password;
                    this.Phone = item.Phone;
                    this.RealName = item.RealName;
                    this.Sex = item.Sex;
                    this.Token = item.Token;
                    this.TokenDate = item.TokenDate;
                    this.UserName = item.UserName;
                    this.Role = item.Role;
                    this.Collage = item.Collage;
                    this.RegisterDate = item.RegisterDate;
                    this.QQ = item.QQ;
                    return this;
                }
                return null;
            }
        }
        #region Fields
        [Column(TypeName="int")]
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int Id { get; set; }

        [Column("UserName",TypeName="varchar")]
        [StringLength(200)]
        public string UserName { get; set; }

        [Column("Password",TypeName="varchar")]
        [StringLength(200)]
        [Required]
        public string Password { get; set; }

        /// <summary>
        /// 昵称，非必须
        /// </summary>
        [Column("NickName",TypeName="nvarchar")]
        [StringLength(200)]
        public string NickName { get; set; }

        /// <summary>
        /// 是否已经激活
        /// </summary>
        [Column("IsActivate",TypeName="bit")]
        [Required]
        public bool IsActivate { get; set; }

        /// <summary>
        /// Sha1加密的Guid标示
        /// </summary>
        [Column("Token",TypeName="varchar")]
        [StringLength(200)]
        [Required]
        public string Token { get; set; }

        /// <summary>
        /// Token值的生成时间
        /// </summary>
        [Column("TokenDate",TypeName="datetime")]
        [Required]
        public DateTime TokenDate { get; set; }

        /// <summary>
        /// 真实姓名
        /// </summary>
        [Column("RealName",TypeName="nvarchar")]
        [StringLength(45)]
        public string RealName { get; set; }

        /// <summary>
        /// 性别
        /// false:女 true:男
        /// </summary>
        [Column("Sex",TypeName="bit")]
        public bool Sex { get; set; }

        /// <summary>
        /// 现所在区域
        /// </summary>
        [Column("Location",TypeName="nvarchar")]
        [StringLength(200)]
        public string Location { get; set; }

        /// <summary>
        /// 所在学校
        /// </summary>
        [Column(TypeName="int")]
        public int Collage { get; set; }

        /// <summary>
        /// 联系电话
        /// </summary>
        [Column("Phone",TypeName="varchar")]
        [StringLength(45)]
        public string Phone { get; set; }
        
        /// <summary>
        /// 头像
        /// </summary>
        [Column("Image", TypeName = "nvarchar")]
        [StringLength(200)]
        public string Image { get; set; }

        /// <summary>
        /// 角色 false：学生 true：企业
        /// </summary>
        [Column(TypeName="bit")]
        public bool Role { get; set; }

        /// <summary>
        /// 是否被挂起（可能登陆）
        /// </summary>
        [Column(TypeName="bit")]
        public bool IsSuspend { get; set; }

        [Column(TypeName="datetime")]
        public DateTime RegisterDate { get; set; }

        [Column(TypeName="varchar")]
        [StringLength(15)]
        public string QQ { get; set; }
        #endregion
        public override int PostModify()
        {
            if (!String.IsNullOrEmpty(this.UserName))
            {
                ueCache.RemoveCache("GetUser:" + this.UserName);
            }
            return base.PostModify();
        }
    }
}
